package com.FH.applyForm.officeServies.officeToolsThird_Type;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/8.
 */
@Stateless
public class OfficeToolsImpl extends AccessDB implements OfficeToolsDAO {


    /**
     * 查询办公用品信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getofficeToolsInfo(String search, String page, String size) {
        ResultDTO resultDTO=new ResultDTO();
        try{
            JSONObject json=new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from officetools_manage where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if(page.equals("1")){
                sql_page = " limit 0,"+size+"";
            }else{
                sql_page = " limit "+((page_ok-1)*size_ok+1)+","+size+"";
            }

            if(json.has("office_id")){
                //sql_w += " AND sys_menu_id = "+json.getString("sys_menu_id");
                sql_w += " AND office_id ='" + json.getString("office_id") + "'";
            }
            if(json.has("officetools_id")){
                //sql_w += " AND sys_menu_id = "+json.getString("sys_menu_id");
                sql_w += " AND officetools_id ='" + json.getString("officetools_id") + "'";
            }



            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String []> seresult = new ArrayList<String []>();

            List<OfficeToolsShowDTO> result = new ArrayList<OfficeToolsShowDTO>();

            if(!existName.equals("0")){
                String sql="select officetools_id,officetools_name,officetools_price,office_id,officetools_number from officetools_manage where 1=1"+sql_w +sql_page;
                seresult=this.getReportingTo(sql);

                for(int i=0;i<seresult.size();i++){

                    OfficeToolsShowDTO sysmenuShowDTO=new OfficeToolsShowDTO();
                    sysmenuShowDTO.setOfficetools_id(seresult.get(i)[1]);
                    sysmenuShowDTO.setOfficetools_name(seresult.get(i)[2]);
                    sysmenuShowDTO.setOfficetools_price(seresult.get(i)[3]);
                    sysmenuShowDTO.setOffice_id(seresult.get(i)[4]);
                    sysmenuShowDTO.setOfficetools_number(seresult.get(i)[5]);

                    result.add(sysmenuShowDTO);
                }
            }

            if(seresult.size()!=0) {
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultDTO;
    }


    /**
     * 增加办公用品信息
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO addofficeToolsInfo(OfficeToolsPullDTO dataDTO) {

        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();

        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");


        String officetools_name=dataDTO.getOfficetools_name();
        String sql="select * from officetools_manage where officetools_name='"+officetools_name+"'";
        String  sum=getReportingToOne(sql);
        if(!sum.equals("")){

            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("信息已存在");
            return resultDTO;
        }


        String Orgrole_insert="";
        Orgrole_insert="insert into officetools_manage (officetools_id,officetools_name,officetools_price,office_id,officetools_number)" +
                "values('"+uuid+"','"+dataDTO.getOfficetools_name()+"','"+dataDTO.getOfficetools_price()+"','"+dataDTO.getOffice_id()+"','"+dataDTO.getOfficetools_number()+"')";

        resultls=addReportingTo(Orgrole_insert);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改办公用品信息
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO updateofficeToolsInfo(OfficeToolsPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        //更新sql语句
        QUERY += "update officetools_manage set officetools_id = '" + dataDTO.getOfficetools_id()+ "'";
        if(null!=dataDTO.getOfficetools_name()&&!"".equals(dataDTO.getOfficetools_name())){
            QUERY += ", officetools_name = '" + dataDTO.getOfficetools_name() + "'";
        }
        if(null!=dataDTO.getOfficetools_price()&&!"".equals(dataDTO.getOfficetools_price())){
            QUERY += ", officetools_price = '" + dataDTO.getOfficetools_price() + "'";
        }
        if(null!=dataDTO.getOfficetools_number()&&!"".equals(dataDTO.getOfficetools_number())){
            QUERY += ", officetools_number = '" + dataDTO.getOfficetools_number() + "'";
        }


        QUERY += " where officetools_id = '" + dataDTO.getOfficetools_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除办公用品信息
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO deleteofficeToolsInfo(OfficeToolsPullDTO dataDTO) {

        //删除之前首先查询该表中是否有记录

        String sql="select count(1) from officetools_manage WHERE officetools_id='"+dataDTO.getOfficetools_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from officetools_manage  where officetools_id = '"+dataDTO.getOfficetools_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

}
